package com.ruxiaoxin.share.domain.user;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Sets;

/**
 * @author ruxiaoxin
 */
public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	private String id;
	private String userName; 						// 用户名
	private String password; 						// 用户密码
	private String email;
	private Long registered;						// 注册时间
	private Integer isValid;						// 是否生效

	private List<Role> roles = new ArrayList<Role>(); // 角色

	public User() {
	};

	public boolean isAdmin() {
		return !(isBadUser());
	}

	public boolean isBadUser() {
		for (Role role : getRoles()) {
			if (role == null || role.getId() == null) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 是否有角色
	 * 
	 * @return
	 */
	public boolean hasRole() {
		return getRoles().isEmpty() ? false : true;
	}

	public boolean hasRole(String roleName) {
		if (hasRole()) {
			for (Role one : getRoles()) {
				if (one.getRoleName() != null
						&& one.getRoleName().equals(roleName)) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 获得用户的角色
	 * 
	 * @return
	 */
	public Set<String> getRolesAsString() {
		Set<String> rolesAsString = Sets.newHashSet();
		List<Role> roles = getRoles();
		if (!roles.isEmpty()) {
			for (Role role : roles) {
				rolesAsString.add(role.getRoleName());
			}
		}
		return rolesAsString;
	}

	/**
	 * 获得用户的权限
	 * 
	 * @return
	 */
	public Set<String> getPermissionesAsString() {
		Set<String> permissionesAsString = Sets.newHashSet();
		List<Role> roles = getRoles();
		if (!roles.isEmpty()) {
			for (Role role : roles) {
				List<Permission> permissiones = role.getPermissiones();
				if (!permissiones.isEmpty()) {
					for (Permission permission : permissiones) {
						permissionesAsString.add(permission.getExpression());
					}
				}
			}
		}
		return permissionesAsString;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public User setRoles(List<Role> roles) {
		this.roles = roles;
		return this;
	}

	public String getUserName() {
		return userName;
	}

	public User setUserName(String userName) {
		this.userName = userName;
		return this;

	}

	public String getPassword() {
		return password;
	}

	public User setPassword(String password) {
		this.password = password;
		return this;
	}

	
	public String getEmail() {
		return email;
	}

	public User setEmail(String email) {
		this.email = email;
		return this;
	}

	public Integer getIsValue() {
		return isValid;
	}

	public User setIsValue(Integer isValid) {
		this.isValid = isValid;
		return this;
	}

	public Long getRegisterTime() {
		return registered;
	}

	public User setRegisterTime(Long registered) {
		this.registered = registered;
		return this;
	}

	public User setId(String id) {
		this.id = id;
		return this;
	}

	public String getId() {
		return id;
	}

}
